# coding=utf-8
from rest_framework.views import APIView
from rest_framework.response import Response
from fe_api.imgrecog.utils import viewutils


class WordRecog(APIView):

    def post(self, request):
        base64img = request.data.get('base64img')
        r_txt = request.data.get('r_txt')
        hw_stroke = request.data.get('hw_stroke', 0)
        if not base64img:
            return Response({'status': 400})
        if str(request.user) == 'AnonymousUser':
            return Response({'status': 403})
        res, txt = viewutils.judge_word_txt(
            r_txt, viewutils.base64_to_img(base64img),
            hw_stroke, request.user.uid)
        if res is True:
            return Response({
                'status': 200,
                'data': {'recg': 1, 'txt': txt}})
        return Response({
            'status': 200,
            'data': {'recg': 0, 'txt': txt}})
